﻿using SP.Service.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using SP.Domain.IRepository;
using SP.Domain.Model;
using System.Data;


namespace SP.EFcore.Repository
{
    public class UserRepository : Repository<UserTable>, IUserRepository
    {
        //private readonly MyDbContext _db;
        //public UserRepository(MyDbContext db):base(db)
        //{
        //    _db = db;
        //}
        protected DapperFactory _db = DapperFactory.GetInstance();
        //批量删除
        public int BatckUser(string id)
        {
            var asd = 0;
            string[] ids = id.Split(',');
            foreach (var item in ids)
            {
                string sql = "delete from sp_usertable where Id=@Id";
                var list = _db.GetDbConnection().Execute(sql, new { Id = item });
                asd++;
            }
            return asd;
        }

        //用户反填
        //public List<UserTable> BackUserList(int id)
        //{
        //    string sql = "select * from sp_usertable where Id=@Id";
        //    var list=_db.GetDbConnection.query
        //}

        public List<UserTable> GetAll()
        {
            return null;
        }
        /// <summary>
        /// 用户修改
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="PassWord"></param>
        /// <returns></returns>
        public UserTable Login(string UserName, string PassWord)
        {
            var pass = MD5Hash.Hash.Content(PassWord);
            using var conn=_db.GetDbConnection();
            string strsql = "select * from sp_usertable where UserName=@UserName && PassWord=@PassWord && Isdel=false";
            UserTable result = conn.QuerySingleOrDefault<UserTable>(strsql, new { UserName=UserName, PassWord = pass });

            //var list = _db.Set<UserTable>().Where(s => s.UserName.Equals(UserName) && s.PassWord.Equals(MD5Hash.Hash.GetMD5(PassWord)) && s.Isdel == false).FirstOrDefault();
            if (result != null)
            {
                return result;
            }
            return null;
        }
        //调用存储过程分页
        public PageModel<UserTable> USerPageList(int pageIndex, int pageSize, string? UserName)
        {
            var str = "";
            if (!string.IsNullOrEmpty(UserName))
            {
                str += $" and UserName like '%{UserName}%'";
            }
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("_pageIndex", pageIndex);
            parameter.Add("_pageSize", pageSize);
            parameter.Add("_tableName", "sp_usertable");
            parameter.Add("_condition", str);
            parameter.Add("_totalCount", dbType: DbType.Int32, direction: ParameterDirection.Output);
            using var conn = _db.GetDbConnection();
            var list = conn.Query<UserTable>("p_UserList", commandType: CommandType.StoredProcedure,param:parameter).ToList();
            int total = parameter.Get<int>("_totalCount");
            PageModel<UserTable> pagelist = new()
            {
                Total = total,
                List = list
            };
            return pagelist;
        }
        //public int Delete(int id)
        //{
        //    string strsql = "update sp_usertable set Isdel=true where Id=@id";
        //    int result = _db.ExecuteScalar<int>(strsql, new { Id = id });
        //    return result;

        //}

        //public List<UserTable> GetList()
        //{
        //    string sql = "select * from sp_usertable where Isdel=false";
        //    return _db.Query<UserTable>(sql).ToList();
        //}

        //public int Insert(UserTable model)
        //{
        //    string sql = "insert sp_usertable values(UserName=@UserName,PassWord=@PassWord,)";
        //    int result = _db.ExecuteScalar<int>(sql, new { UserName = model.UserName, PassWord = MD5Hash.Hash.GetMD5(model.PassWord)});
        //    return result;
        //}

        //public int Update(UserTable model)
        //{
        //    string sql = "update sp_usertable set UserName=@UserName,PassWord=@PassWord, where Id=@Id";
        //    int result = _db.ExecuteScalar<int>(sql, new { UserName = model.UserName, PassWord = model.PassWord,Id=model.Id });
        //    return result;
        //}
    }
}
